package com.example.apigateway.config;

import org.apache.catalina.filters.CorsFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * 跨域配置
 */
@Configuration
public class CorsConfig {
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Arrays.asList("*"));//www.a.com
        config.setAllowedHeaders(Arrays.asList("*"));//允许的头
        config.setAllowedMethods(Arrays.asList("*"));//请求方式
        config.setMaxAge(300L);//在300s里，相同的请求不会再检测
        source.registerCorsConfiguration("/**",config);
        return new CorsFilter();
    }
}
